package cn.sowfin.controller;

import cn.sowfin.entity.User;
import cn.sowfin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("")
    public Map<String,Object> create(
            @RequestParam String username,@RequestParam String password,@RequestParam String nickname
    ){
        User user=new User().setUsername(username).setPassword(password).setNickname(nickname).setAddTime(new Date());
        userService.create(user);
        Map<String,Object> result=new HashMap<>();
        return result;
    }
    @PutMapping("")
    public Map<String,Object> update(
            @RequestParam Integer id,
            @RequestParam String nickname

    ){
        User user=new User().setId(id).setNickname(nickname);
        userService.save(user);
        Map<String,Object> result=new HashMap<>();
        result.put("ok",true);
        return result;
    }

    @PostMapping("/{id}")
    public Map<String,Object> delete(@PathVariable("id") Integer id){

        userService.remove(id);

        Map<String,Object> result=new HashMap<>();
        result.put("ok",true);
        return result;
    }
    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }


    @GetMapping("/get/{id}")
    public User get(@PathVariable("id") Integer id){
        return userService.get(id);
    }



}
